package com.chunfeng.mypen.controller;

import com.chunfeng.mypen.entity.User;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户Controller
 *
 * @author by 春风能解释
 * <p>
 * 2023/11/19
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户控制层", tags = "用户访问操作接口")
public class UserController {

    /**
     * 用户业务层注入
     */
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param name     用户名
     * @param password 密码
     * @return JSON
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public JsonRequest<String> login(
            @ApiParam(value = "用户名", required = true)
            @RequestParam String name,
            @ApiParam(value = "密码", required = true)
            @RequestParam String password) {
        return userService.login(name, password);
    }

    /**
     * 用户注册
     *
     * @param name     用户名
     * @param password 密码
     * @return JSON
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册")
    public JsonRequest<Integer> register(
            @ApiParam(value = "用户名", required = true)
            @RequestParam String name,
            @ApiParam(value = "密码", required = true)
            @RequestParam String password) {
        return userService.register(name, password);
    }


    /**
     * 条件查询用户
     *
     * @param current 当前页
     * @param size    每页长度
     * @param user    条件
     * @return JSON
     */
    @GetMapping
    @ApiOperation(value = "条件查询用户")
    public JsonRequest<List<User>> lookAllUser(
            @ApiParam(value = "当前页数", required = true)
            @RequestParam Integer current,
            @ApiParam(value = "每页长度", required = true)
            @RequestParam Integer size,
            @ApiParam(value = "条件", required = true)
            User user) {
        return userService.lookAllUser(current, size, user);
    }

    /**
     * 查询用户个人信息
     *
     * @param id 用户ID
     * @return JSON
     */
    @GetMapping("/one")
    @ApiOperation(value = "查询用户个人信息")
    public JsonRequest<User> lookOneUser(
            @ApiParam(value = "用户ID", required = true)
            @RequestParam String id) {
        return userService.lookOneUser(id);
    }

    /**
     * 更新用户信息
     *
     * @param user 待更新的用户
     * @return JSON
     */
    @PutMapping
    @ApiOperation(value = "更新用户信息")
    public JsonRequest<Integer> updateOneUser(
            @ApiParam(value = "待更新的用户", required = true)
            @RequestBody User user) {
        return userService.updateOneUser(user);
    }


    /**
     * 批量注销用户
     *
     * @param ids 用户ID
     * @return JSON
     */
    @DeleteMapping
    @ApiOperation(value = "批量注销用户")
    public JsonRequest<Integer> logoutUsers(
            @ApiParam(value = "用户ID", required = true)
            @RequestBody List<String> ids) {
        return userService.logoutUsers(ids);
    }
}
